#include "Common.h"
#include "Mutex.h"
#define WIN32_LEAN_AND_MEAN  // We only need minimal includes
#include <windows.h>

struct Mutex::Internal
{
	CRITICAL_SECTION mutex;
#ifndef NDEBUG
	DWORD thread_id;
#endif
};

Mutex::Mutex() : mInternal(new Internal())
{
	InitializeCriticalSection(&mInternal->mutex);
}

Mutex::~Mutex() 
{
	DeleteCriticalSection(&mInternal->mutex);
	delete mInternal;
}

void Mutex::Lock() 
{
	EnterCriticalSection(&mInternal->mutex);
#ifndef NDEBUG
	mInternal->thread_id = GetCurrentThreadId();
#endif
}

void Mutex::Unlock() 
{
#ifndef NDEBUG
	mInternal->thread_id = 0;
#endif
	LeaveCriticalSection(&mInternal->mutex);
}

void Mutex::AssertHeld()
{
#ifndef NDEBUG
	assert(mInternal->thread_id == GetCurrentThreadId());
#endif
}